#include "SprQuad.h"
#include <cassert>
#include <algorithm>

SprQuad::SprQuad(int id, int timeline,float width, float height, const sf::Vector2f& t1, const sf::Vector2f& t2,
	const sf::Vector2f& t3, const sf::Vector2f& t4, const sf::Vector2f& origin , float depth)
	: Quad(id,timeline),	
	mV1(sf::Vector2f(width, height), t1),
	mV2(sf::Vector2f(width, 0.f), t2),
	mV3(sf::Vector2f(0.f, 0.f), t3),
	mV4(sf::Vector2f(0.f, height), t4),
	mDepth(depth)
{
	mType = Quad::Type::Sprite;
	setOrigin(origin);
}

//sets new sprite 

void SprQuad::setTexCoord(const sf::Vector2f& t1, const sf::Vector2f& t2,
	const sf::Vector2f& t3, const sf::Vector2f& t4)
{
	mV1.texCoords = t1;
	mV2.texCoords = t2;
	mV3.texCoords = t3;
	mV4.texCoords = t4;
}

// mirrors texture for both directions left/right

void SprQuad::mirrorTexture()
{
	sf::Vector2f t3Temp = mV3.texCoords;
	sf::Vector2f t4Temp = mV4.texCoords;
	mV3.texCoords = mV2.texCoords;
	mV4.texCoords = mV1.texCoords;
	mV1.texCoords = t4Temp;
	mV2.texCoords = t3Temp;
}

sf::Vertex& SprQuad::operator[](int i)
{
	return getVertex(i);
}

// gets Vertex Counter Clockwize 0->down,right 1->up,right 2->up,left 3->down,left

sf::Vertex& SprQuad::getVertex(int i)
{
	switch (i)
	{
	case 0:
		return mV1;
		break;
	case 1:
		return mV2;
		break;
	case 2:
		return mV3;
		break;
	case 3:
		return mV4;
		break;
	default:
		return mV1;
	}
}

sf::Vertex SprQuad::getWorldVertex(int vertexNum)
{
	sf::Vertex vertex;
	vertex.position = getWorldTransform() * getVertex(vertexNum).position;
	vertex.texCoords = getVertex(vertexNum).texCoords;
	return vertex;
}

//Sets new positions for vertices and origin

void SprQuad::setDimensions(float newWidth, float newHeight, sf::Vector2f newOrigin)
{
	mV1.position = sf::Vector2f(newWidth, newHeight);
	mV2.position = sf::Vector2f(newWidth, 0.f);
	mV3.position = sf::Vector2f(0.f, 0.f);
	mV4.position = sf::Vector2f(0.f, newHeight);
	setOrigin(newOrigin);
}

bool SprQuad::operator<(const SprQuad& rho)
{
	return this->mDepth < rho.mDepth;
}

void SprQuad::setDepth(float depth)
{
	mDepth = depth;
}

float SprQuad::getDepth() const
{
	return mDepth;
}
